package com.angel.web.app;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.aspectj.weaver.bcel.BcelPerClauseAspectAdder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.Constants;
import com.angel.core.base.JsonEntity;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.DateUtils;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.SMSUtils;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzCustomerLog;
import com.angel.health.BuzReserve;
import com.angel.health.BuzServicePackages;
import com.angel.health.BuzVideo;
import com.angel.health.SMS;
import com.angel.provider.app.WangYiProvider;
import com.angel.provider.health.BuzCustomerLogProvider;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzReserveProvider;
import com.angel.provider.health.BuzServicePackagesProvider;
import com.angel.provider.health.BuzVideoProvider;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年9月28日 下午4:47:07 
* 类说明  app预约服务
*/
@Controller
@RequestMapping("app/reserve")
public class AppReserveController {
	
	@Autowired
	private BuzReserveProvider brProvider;
	@Autowired
	private BuzVideoProvider bvProvider;
	@Autowired
	private BuzServicePackagesProvider bpProvider;
	@Autowired
	private BuzCustomerProvider bcProvider;
	@Autowired
	private WangYiProvider wyProvider;
	@Autowired
	private BuzCustomerLogProvider bclProvider;

	/**
	 * 新增预约
	 * @param buzReserve
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="add",produces="application/json; charset=utf-8")
	public String addReserve(BuzReserve buzReserve,BuzCustomer buzCustomer){
		if(buzReserve.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确！"));
		}
		buzCustomer.setId(buzReserve.getCustomerId());
		int insert = brProvider.insert(buzReserve);
		if(insert > 0){
			//新增预约日志
			BuzCustomerLog bcl = new BuzCustomerLog();
			if("1".equals(buzReserve.getPackageType())){
				bcl.setContent("您预约服务套餐成功！");
				bcl.setType(2);
			}else{
				bcl.setContent("您购买体检套餐成功！");
				bcl.setType(3);
			}
			bcl.setCustomerId(buzReserve.getCustomerId());
			bclProvider.insert(bcl);
			
			//更新用户信息
			bcProvider.updateById(buzCustomer);
			//若为支付状态给用户发信息
			if("1".equals(buzReserve.getPayStatus())){
				buzCustomer = bcProvider.findById(buzCustomer);
				String single = Constants.SEND_SMS_SINGLE;
				String content = "尊敬的用户，您于"+DateUtils.getNowTimeForStr()+"预约套餐已成功！【天使健康】";
				SMSUtils.send(
						new SMS(buzCustomer.getId().toString(), buzCustomer.getMemPhone(), content, buzCustomer.getId().toString(), ""),
						single);
			}
			
			//更新预约编号
			buzReserve.setCode(CodeUtil.createCode(null, Constants.CUSTOMERSERVICENUM, buzReserve.getId()));
			brProvider.updateById(buzReserve);
			
			if("1".equals(buzReserve.getPayStatus()) && "1".equals(buzReserve.getPackageType())){
				BuzServicePackages bsp = new BuzServicePackages();
				bsp.setId(buzReserve.getServicePackageId());
				String videoTime = bpProvider.findById(bsp).getVideoTime();
				BuzVideo bv = new BuzVideo();
				int _videoTime = 0;
				try{
				_videoTime = Integer.parseInt(videoTime);
				}catch(Exception e){
				}
				bv.setVideoMinutes(_videoTime);
				bv.setCustomerId(buzReserve.getCustomerId());
				bv.setCreateTime(new Date());
				bvProvider.insert(bv);
			}
			buzCustomer = bcProvider.findById(buzCustomer);
			try {
				String updateUinfo = wyProvider.updateUinfo(JsonUtil.toJson(buzCustomer));
				System.err.println(updateUinfo);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
			}
			return JsonUtil.toJson(new JsonEntity("200", buzCustomer));
		}
		
		return JsonUtil.toJson(new JsonEntity("-1", "预约失败！"));
	}
	
	/**
	 * 查看预约
	 * @param buzReserve
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="index",produces="application/json; charset=utf-8")
	public String index(BuzReserve buzReserve){
		if(buzReserve.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1","参数不正确"));
		}
		buzReserve.setEnable(1);
		List<Map<String,Object>> list = brProvider.findReserveAll(buzReserve);
		return JsonUtil.toJson(new JsonEntity("200",list));
	}
}
